package com.moon.game.command.guest.player;

import org.apache.log4j.Logger;

import com.moon.game.clazz.PlayerItemsProto.PlayerItems.PlayerItem;
import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.exception.GException;
import com.moon.game.service.PlayerItemService;
import com.moon.game.service.PlayerService;
import com.moon.game.vos.PlayerVo;

/**
 * @description 卸下装备
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-5-24 上午10:59:25
 */
public class TakeOffEquipCmd extends BaseCmd{
	private static final Logger log = Logger.getLogger(TakeOffEquipCmd.class);
	
	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
		case STATU_INIT:
			doInit(req);
			break;
		case STATU_CHECK:
			doCheckSelf(req);
			break;
		case STATU_RETURN:
			doResponse(req);
			break;
		default:
			break;
		}
	}

	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null) {
			doResponsePsError(req);
			return;
		}
		long playerItemId = Long.parseLong(ps[0]);
		PlayerVo player = getPlayerVo(req);
		int ret = PlayerItemService.takeOffTheEquip(player, playerItemId);
		String info = "";
		if (ret == 0) {
			info = "装备卸载成功！";
//			UpdatePlayerThread.addSyncPlayerMap(player);
			PlayerService.doUpdatePlayer(player.getSid(), player.getId(), player);
		}else {
			info = "装备卸载失败，您的装备已经不在身上了！";
		}
		
		PlayerItem playerItem = PlayerItemService.getPlayerItem(req.getPlayerId(), playerItemId);
		if (playerItem == null) {
			log.info(player.getId() + ":take off equip fail,player item id is[" + playerItemId + "]");
		}else {
			PlayerItemService.syncPlayerItem(req.getPlayerId(), PlayerItem.newBuilder(playerItem).setIsUse(false).build());
		}
		req.setVO("info", info);
		response(req, req.getClientResponse());
	}

}
